<?php 
	namespace Home\Controller;
	use Think\Controller;

	class ProgramInfoController extends Controller
	{
		public function index(){return;}

		/*
		 * 工单接口处理		
		 */
		/*public function all()
		{
			// -------1.根据用户id查出对应省份节目(剧头)信息------
			$userid = I("get.userid","");
			$limit = I("post.limit","");   //待用
			if($userid != ""){
				// 获取到用户对应省份节目信息
				$res = D("UserProvince")->get_province($userid,$page="",$rows="",$sort="upload_time",$order="",$program_name="",$province_name="");
				// 获取用户所在省份id
				$province_id = $this->get_provinceid($userid);
				// dump($res);exit;   
			}

			// dump($res);exit;    //剧头信息 暂时查取单个进行处理 
			// --------数据处理部分---------
			if($res){
				foreach ($res['res'] as $k => $v) {  
					//   $seriesInfo_movieInfo为数组
					// dump($v["id"]);    // 75、77==> userid=14
					$seriesInfo_movieInfo = $this->get_serierid($v["id"],$v['code_type'],$province_id);  
					dump($seriesInfo_movieInfo);exit();
					//视频+剧集信息  
					foreach ($seriesInfo_movieInfo as $key => $value) {
						if($value["movieInfo"]){
							$picInfo = $this->get_picid($v["id"]);   //视频+剧集信息
							$v['picInfo'] = $picInfo;
							foreach ($seriesInfo_movieInfo as $key => $value) {
								// dump($value);exit();
								$result['Program'] = $v;
								$result['Series'] = $value;
								$output[] = $result;   //一个剧头对应的全部剧集
							}
							// dump($output);exit();
						}else{	
							continue;
						}
					}
						dump($output);
				 		break;
				}
				// 剧集逐个加入剧头信息
			}else{

			}
			// $output['total'] = $res['total']; 
			// $output['rows'] = $res['res']; 				
			// echo json_encode($output);
		}

		//  获取视频信息
		public function get_movieinfo($program_id,$code_type)
		{
			// $map["download_status"] = 2;  //ftp存在的视频
			$map["program_id"] = $program_id; 
			$map["code_rate"] = $code_type; 
			$resMovie = M("Movie")->where($map)->order('id desc')->select();
			$resMovie?$resMovie:$resMovie = "";
			return $resMovie;
		}

		//获取剧头对应==>剧集id数组
		public function get_serierid($program_id,$code_type,$province_id)
		{
			if(!empty($program_id)){
				$seriesInfo = D("Program")->where(["parent_id"=>$program_id])->select();     //获取剧集数组信息
				// dump($seriesInfo);exit;
				foreach ($seriesInfo as $key => $value) {    
				     //做判断是否在已下发的表中   $value['id']为剧集id
				     $result = $this->get_program_exit($value['id'],$province_id);
				     if($result){         //存在true跳过
				     	  continue; 
				     }else{               //不存在查取视频信息
			     		$movieInfo = $this->get_movieinfo($value['id'],$code_type);    //获取视频信息
				 		$seriesInfo[$key]["movieInfo"] = $movieInfo;  
				     }                
				}
				return $seriesInfo;
			}
		}

		public function get_picid($program_id)
		{
			// 根据剧头id获取到图片id
			$map['Picture.parent_id'] = $program_id;
			// $map['Picture.type'] = 2;
			$res = D("Picture")->join("picture_Info ON Picture.picture_id = picture_Info.id","RIGHT")->where($map)->select();
			$res?$res:"";
			return $res;
		}

		public function get_program_exit($program_id,$province_id)
		{
			// 判断剧集对应的id是否在t_download表中存在
			$res = D("TDownload")->where(["program_id"=>$program_id])->find();  
			if(!empty($res) && $res){
				return true;     //存在
			}else{
				$data['province_id'] = $province_id;
				$data['program_id'] = $program_id;
				$data['create_time'] = time();
				$data['update_time'] = time();
				$data['download_status'] = 0;    //初始化为0
				$res = D("TDownload")->add($data);
				return false;    //不存在
			}
		}

		public function get_provinceid($userid)
		{
			$res = D("UserProvince")->where(["user_id"=>$userid])->find();
			return $res['province_id'];
		}*/

		/**
		 * 工单分发接口
		 * @return [type] [description]
		 */
		private function all_one($userid)
		{
			if($userid && $userid != ""){
				 $seriers = $this->get_seriers($userid);    //获取剧集
				 if($seriers){
				 	 $code_rate = $this->get_coderate($userid);   //获取码率
					 $movie = $this->get_movie($seriers['id'],$code_rate);   //获取视频
					 $program = $this->get_program($seriers['id']);             //获取剧头
					 $output = $program;
					 $output['seriers'] = $seriers;
					 $output['movie'] = $movie;
				 }else{
				 	$output['code'] = '404';
				 	$output['message'] = '未找到数据';
				 }
				 return $output;
			}else{
				$result = [
					"coed"=>"404",
					"message"=>"参数错误"
				];
				return $result;
			}
		}

		/**
		 * 满足条件剧集
		 * @param  [type] $userid [description]
		 * @return [type]         [description]
		 */
		private function get_seriers($userid)
		{
			$Model = new \Think\Model();
   			$seriers = $Model->query("SELECT * from t_program  as c WHERE c.parent_id in(SELECT b.program_id from program_province as b WHERE b.province_id in(SELECT a.province_id FROM user_province as a WHERE a.user_id = {$userid}))and c.id NOT IN (SELECT program_id FROM t_download) LIMIT 1;");
   			if($seriers && !empty($seriers)){
   				// 当前节目插入到下载表中
   				$program_id = $seriers[0]['id'];
   				// 可用可不用
   				$province_id = D("UserProvince")->where(["user_id"=>$userid])->field("province_id")->find();
   				$province_ids = $province_id['province_id'];
   				$data['province_id'] = $province_ids;
   				$data['program_id'] = $program_id;
   				$data['create_time'] = time();
   				$data['update_time'] = time();
   				$data['download_status'] = 0;
   				$res = D("TDownload")->add($data);
   				if($res){
		   			return $seriers[0];  
   				}else{
   					return $result = "插入失败";
   				}
   			}else{
   				return $result = false;
   			}
		}

		/**
		 * 用户对应省份的码率
		 */
		private function get_coderate($userid)
		{	
			$code_rate = D("UserProvince")->join("t_province on user_province.province_id = t_province.id")->where(["user_province.user_id"=>$userid])->field("type")->find();
			if($code_rate && !is_null($code_rate)){
				return $code_rate['type'];
			}else{
				return;
			}
		}

		/**
		 * 视频信息
		 */
		private function get_movie($program_id,$code_rate)
		{
			$map["program_id"] = $program_id;
			$map["code_rate"] = $code_rate;
			$movie = D("Movie")->where($map)->find();
			if($movie && !empty($movie)){
				return $movie;
			}else{
				return;
			}
		}

		/**
		 * 剧头信息
		 */
		private function get_program($program_id)
		{
			$parent_id = D("Program")->where(["id"=>$program_id])->field("parent_id")->find();  
			$program = D("Program")->where(["id"=>$parent_id['parent_id']])->find();
			$picture = $this->get_picture($parent_id['parent_id']);
			$result['program'] = $program;  
			$result['picture'] = $picture;  
			return $result;
		}

		/**
		 * 图片信息
		 */
		private function get_picture($program_id)
		{	
			$Model = new \Think\Model();
   			$picture = $Model->query("SELECT * FROM picture_info WHERE id IN(SELECT picture_id from picture WHERE parent_id = {$program_id})");
   			return $picture;
		}

		/**
		 * 公共接口
		 */
		public function all()
		{
			// 接收到参数
			$userid = I("get.userid","");
			$limit = I("get.limit","");
			$time = I("get.time","");
			$token = I("get.token","");
			$userid = intval($userid);
			$limit = intval($limit);
			if($userid && $limit && $time && $token){
				// 来源认证
				// $res = $this->check($userid,$time,$token);
				$res = true;
				if($res){
					for ($i=0; $i < $limit; $i++) { 
						$result[] = $this->all_one($userid);
					}	
				}else{
					$result = [
						"code"=>401,
						"message"=>"来源认证失败"
					];
				}
			}else{
				$result = [
					"code"=>404,
					"message"=>"参数错误"
				];
			}
			echo json_encode($result);
		}

		/**
		 * 下载反馈接口
		 */
		public function feedback()
		{
			$province_id = intval(I("get.province_id",""));
			$program_id = intval(I("get.program_id",""));
			$download_status = intval(I("get.download_status",""));
			if($province_id && $program_id && $download_status && $userid){
				$res = $this->check($userid,$time,$token);
				if($res){
					$map['province_id'] = $province_id;
					$map['program_id'] = $program_id;
					$data['download_status'] = $download_status;
					$data['update_time'] = time();
					$res_update = D("TDownload")->where($map)->save($data);
					if($res_update){
						$output = [
							"code"=>'200',
							"message"=>"更新成功"
						];
					}else{
						$output = [
							"code"=>'201',
							"message"=>"更新失败"
						];
					}
				}else{
						$output = [
							"code"=>'401',
							"message"=>"来源认证失败"
						];
				}
			}else{
				$output = [
					"code"=>404,
					"message"=>"参数错误"
				];
			}
				echo json_encode($output);
		}

		/**
		 * 来源认证
		 * @return [type] [description]
		 */
		public function check($userid,$time,$token)
		{
			S("userid_".$userid)?S("userid_".$userid):S("userid_".$userid,0);   //时间缓存
			S("userid_".$useridToken,md5($userid.$time."ngOtUBy1ZKN1dwmm"));  //江西token缓存
			// dump(md5($userid.$time."ngOtUBy1ZKN1dwmm"));
			// dump($time);dump(S("userid_".$userid));dump($token);dump(S("userid_".$useridToken));exit();
			if(($time > S("userid_".$userid)) && ($token == S("userid_".$useridToken))){  
				S("userid_".$userid,$time);
				return true;
			}else{
				return false;
			}
		}

		public function test()
		{
			$time = "1525835449";
			dump(md5("14".$time."ngOtUBy1ZKN1dwmm"));
		}
		
	}